package com.substation.ui.activity;

import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.bean.LoginInfo;
import com.greenentity.DataDictionary;
import com.greenentity.Logger;
import com.greenentity.Substation;
import com.interfaces.ResponseListener;
import com.substation.R;
import com.substation.greendao.DataDictionaryDao;
import com.substation.utils.DateUtils;
import com.substation.utils.DialogUtils;
import com.substation.utils.GreenDaoManager;
import com.substation.utils.TestingUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import butterknife.BindView;
import butterknife.OnClick;

public class LoggerAddActivity extends BaseActivity {
    List<Substation> substations;
    List<DataDictionary> dataDictionaries;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.spinner1)
    Spinner spinner1;
    @BindView(R.id.spinner2)
    Spinner spinner2;
    @BindView(R.id.content)
    EditText content;
    String guid = "";
    DataDictionary dataDictionary;

    Logger logger;
    boolean isUpdate = false;
    Date addDate = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_logger_add;
    }

    @Override
    protected void initView() {
        logger = (Logger) getIntent().getSerializableExtra("logger");
        addDate = new Date();
        time.setText(DateUtils.getDateToStringNoHH(addDate));
        substations = GreenDaoManager.getInstance().getmDaoSession().getSubstationDao().loadAll();
        guid = substations.size() > 0 ? substations.get(0).getGuid() : "";
        List<String> sp1 = new ArrayList<>();
        for (Substation substation : substations) {
            sp1.add(substation.getNames());
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_txt, sp1);
        adapter.setDropDownViewResource(R.layout.spinner_txt);
        spinner1.setAdapter(adapter);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                guid = substations.get(i).getGuid();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        dataDictionaries = GreenDaoManager.getInstance().getmDaoSession().getDataDictionaryDao().queryBuilder()
                .where(DataDictionaryDao.Properties.Type.eq("logs_nature")).list();
        dataDictionary = dataDictionaries.size() > 0 ? dataDictionaries.get(0) : null;
        List<String> sp2 = new ArrayList<>();
        for (DataDictionary dataDictionary : dataDictionaries) {
            sp2.add(dataDictionary.getValue());
        }
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.spinner_txt, sp2);
        adapter2.setDropDownViewResource(R.layout.spinner_txt);
        spinner2.setAdapter(adapter2);
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                dataDictionary = dataDictionaries.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        if (logger != null) {
            isUpdate = true;
            content.setText(logger.getLogsContent());
            int i = 0;
            for (Substation substation : substations) {
                if (substation.getGuid().equals(logger.getSubsGuids())) {
                    spinner1.setSelection(i);
                    break;
                }
                i++;
            }
            i = 0;
            for (DataDictionary dataDictionary : dataDictionaries) {
                if (dataDictionary.getCode().equals(logger.getLogsNature())) {
                    spinner2.setSelection(i);
                }
                i++;
            }
            time.setText(DateUtils.getDateToString(logger.getAddTime()));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_save, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.upload:
                String contentS = content.getText().toString();
                if (!TestingUtils.isEmpty(context, contentS, "日志内容")) return true;
                if (logger == null) {
                    logger = new Logger();
                    logger.setLogsId(System.currentTimeMillis());
                    logger.setLogsGuid(UUID.randomUUID().toString());
                    logger.setStatus("新增");
                    logger.setUserId(LoginInfo.getInfo(context).getAccountProfileDetails().getObjectId());
                    logger.setUserName(LoginInfo.getInfo(context).getAccountProfileDetails().getNickname());
                }
                logger.setAddTime(addDate);
                logger.setLogsContent(contentS);
                logger.setLogsNature(dataDictionary.getCode());
                logger.setLogsNaturevalue(dataDictionary.getValue());
                logger.setSubsGuids(guid);
                if (isUpdate)
                    GreenDaoManager.getInstance().getmDaoSession().getLoggerDao().update(logger);
                else
                    GreenDaoManager.getInstance().getmDaoSession().getLoggerDao().insert(logger);
                finish();
                return true;
            case android.R.id.home:
                DialogUtils.showMsgDialog(context, "是否返回", "您还未保存？是否直接退出？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                });
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.time)
    public void onViewClicked() {
        DialogUtils.showCalendarDialog(getFragmentManager(), "记录时间", new ResponseListener<String>() {
            @Override
            public void response(String data) {
                addDate = DateUtils.getStringToDateNoHH(data);
                time.setText(data);
            }
        });
    }
}
